﻿

using LeaderCCSLaserUI.Models;
using Newtonsoft.Json;
using Prism.Services.Dialogs;
using System.Windows.Forms;
using System;
using MessageBox = System.Windows.Forms.MessageBox;
using Prism.Commands;
using HalconDotNet;
using ViewROI;
using System.Collections.ObjectModel;

namespace LeaderCCSLaserUI.ViewModels.Dialogs.VisionProgram
{
    public class FunctionPreProcessingDialogViewModel : DialogViewModel
    {
        #region 变量
        private HObject mImage;
        #endregion
        #region 属性
        private HImage cameraImage0;
        public HImage CameraImage0
        {
            get { return cameraImage0; }
            set { SetProperty(ref cameraImage0, value); }
        }
        private HObject cameraAppendHObject0;
        public HObject CameraAppendHObject0
        {
            get { return cameraAppendHObject0; }
            set { SetProperty(ref cameraAppendHObject0, value); }
        }
        private Tuple<string, object> cameraGCStyle0;
        public Tuple<string, object> CameraGCStyle0
        {
            get { return cameraGCStyle0; }
            set { SetProperty(ref cameraGCStyle0, value); }
        }
        private ObservableCollection<ROI> cameraROIList0 = new ObservableCollection<ROI>();
        public ObservableCollection<ROI> CameraROIList0
        {
            get { return cameraROIList0; }
            set { SetProperty(ref cameraROIList0, value); }
        }

        private HMsgEntry cameraAppendHMessage0;
        public HMsgEntry CameraAppendHMessage0
        {
            get { return cameraAppendHMessage0; }
            set { SetProperty(ref cameraAppendHMessage0, value); }
        }
        private PreProcessingParam mPreProcessingParam;
        public PreProcessingParam MPreProcessingParam
        {
            get { return mPreProcessingParam; }
            set { SetProperty(ref mPreProcessingParam, value); }
        }
        #endregion
        #region 方法
        private DelegateCommand openImageCommand;
        public DelegateCommand OpenImageCommand =>
            openImageCommand ?? (openImageCommand = new DelegateCommand(ExecuteOpenImageCommand));
        private DelegateCommand<object> operateCommandCommand;
        public DelegateCommand<object> OperateCommandCommand =>
            operateCommandCommand ?? (operateCommandCommand = new DelegateCommand<object>(ExecuteOperateCommandCommand));
        private DelegateCommand emphasizeActionCommand;
        public DelegateCommand EmphasizeActionCommand =>
            emphasizeActionCommand ?? (emphasizeActionCommand = new DelegateCommand(ExecuteEmphasizeActionCommand));

        void ExecuteEmphasizeActionCommand()
        {
            if (mImage != null)
            {
                var _img = mImage.CopyObj(1, -1);
                HTuple row = new HTuple(0), column = new HTuple(0), angle = new HTuple(0), score = new HTuple(0), v1 = new HTuple(0), v2 = new HTuple(0);
                HObject region, maxRegion;
                HOperatorSet.GenEmptyObj(out region);
                HOperatorSet.GenEmptyObj(out maxRegion);
                var r = MFunc.FunDictionary["PreProcessing"].Run(ref _img, JsonConvert.SerializeObject(MPreProcessingParam, Formatting.Indented), ref row, ref column, ref angle, ref score, ref maxRegion, ref region, ref v1, ref v2);
                region.Dispose();
                maxRegion.Dispose();
                CameraImage0 = new HImage(_img);
            }
            else
            {
                MessageBox.Show("图像不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void ExecuteOperateCommandCommand(object obj)
        {
            switch (obj.ToString())
            {
                case "确认":
                    {
                        try
                        {
                            string jsonString = JsonConvert.SerializeObject(MPreProcessingParam, Formatting.Indented);
                            DialogParameters param = new DialogParameters();
                            param.Add("Parameter", jsonString);
                            OnRequestClose(new Prism.Services.Dialogs.DialogResult(ButtonResult.Yes, param));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    break;
                case "取消":
                    OnRequestClose(new Prism.Services.Dialogs.DialogResult(ButtonResult.Cancel));
                    break;
                default:
                    break;
            }
        }
        void ExecuteOpenImageCommand()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Image Files|*.png;*.bmp;*.jpg;*.tif";
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (mImage != null)
                {
                    mImage.Dispose();
                }
                HOperatorSet.ReadImage(out mImage, openFileDialog.FileName);
                CameraImage0 = new HImage(mImage.CopyObj(1, -1));
            }
        }
        #endregion
        #region 导航
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            try
            {
                var jsonString = parameters.GetValue<string>("Parameter");
                var _image = parameters.GetValue<HObject>("Image");
                if (_image != null)
                {
                    mImage = _image.CopyObj(1, -1);
                    CameraImage0 = new HImage(mImage.CopyObj(1, -1));
                }
                MPreProcessingParam = JsonConvert.DeserializeObject<PreProcessingParam>(jsonString);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MPreProcessingParam = new PreProcessingParam {
                    IsUseEmphasize = false,
                    EmphasizeMaskWidth = 7,
                    EmphasizeMaskHeight = 7,
                    EmphasizeFactor = 2.0
                };
            }
        }
        #endregion
        #region 构造函数
        public FunctionPreProcessingDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            //HOperatorSet.GenEmptyObj(out mImage);
            Title = "预处理功能";
        }
        #endregion

    }
}
